package com.example.myapplication.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.databinding.ItemStockBinding
import com.example.myapplication.model.Stock

class StockAdapter(
    private val onItemClick: (Stock) -> Unit
) : ListAdapter<Stock, StockAdapter.StockViewHolder>(StockDiffCallback()) {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StockViewHolder {
        val binding = ItemStockBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return StockViewHolder(binding, onItemClick)
    }
    
    override fun onBindViewHolder(holder: StockViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
    
    class StockViewHolder(
        private val binding: ItemStockBinding,
        private val onItemClick: (Stock) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(stock: Stock) {
            binding.stockNameTextView.text = stock.name
            binding.stockSymbolTextView.text = stock.symbol
            binding.sectorTextView.text = stock.sector ?: "其他"
            binding.priceTextView.text = "¥%.2f".format(stock.currentPrice)
            
            // 设置涨跌颜色
            val changeColor = if (stock.change >= 0) {
                Color.parseColor("#F44336") // 红色-上涨
            } else {
                Color.parseColor("#4CAF50") // 绿色-下跌
            }
            
            binding.changeTextView.apply {
                text = if (stock.change >= 0) {
                    "+%.2f".format(stock.change)
                } else {
                    "%.2f".format(stock.change)
                }
                setTextColor(changeColor)
            }
            
            binding.changePercentTextView.apply {
                text = if (stock.changePercent >= 0) {
                    "+%.2f%%".format(stock.changePercent)
                } else {
                    "%.2f%%".format(stock.changePercent)
                }
                setTextColor(changeColor)
            }
            
            // 显示买卖信号
            if (!stock.signalType.isNullOrEmpty() && stock.signalType != "HOLD") {
                binding.signalBadge.visibility = android.view.View.VISIBLE
                
                when (stock.signalType) {
                    "BUY", "STRONG_BUY" -> {
                        binding.signalBadge.text = "💰 买入信号"
                        binding.signalBadge.setBackgroundColor(Color.parseColor("#E8F5E9"))
                        binding.signalBadge.setTextColor(Color.parseColor("#2E7D32"))
                        
                        // 显示买入点位
                        if (stock.buyPrice != null && stock.buyPrice > 0) {
                            binding.buyPriceLayout.visibility = android.view.View.VISIBLE
                            binding.buyPriceTextView.text = "¥%.2f".format(stock.buyPrice)
                        } else {
                            binding.buyPriceLayout.visibility = android.view.View.GONE
                        }
                        
                        // 显示目标价格和止损价格
                        if ((stock.targetPrice != null && stock.targetPrice > 0) || 
                            (stock.stopLoss != null && stock.stopLoss > 0)) {
                            binding.priceTargetsLayout.visibility = android.view.View.VISIBLE
                            
                            // 设置目标价格
                            if (stock.targetPrice != null && stock.targetPrice > 0) {
                                binding.targetPriceTextView.text = "¥%.2f".format(stock.targetPrice)
                            } else {
                                binding.targetPriceTextView.text = "暂无"
                            }
                            
                            // 设置止损价格
                            if (stock.stopLoss != null && stock.stopLoss > 0) {
                                binding.stopLossTextView.text = "¥%.2f".format(stock.stopLoss)
                            } else {
                                binding.stopLossTextView.text = "暂无"
                            }
                        } else {
                            binding.priceTargetsLayout.visibility = android.view.View.GONE
                        }
                    }
                    "SELL", "STRONG_SELL" -> {
                        binding.signalBadge.text = "📉 卖出信号"
                        binding.signalBadge.setBackgroundColor(Color.parseColor("#FFEBEE"))
                        binding.signalBadge.setTextColor(Color.parseColor("#C62828"))
                        binding.buyPriceLayout.visibility = android.view.View.GONE
                        binding.priceTargetsLayout.visibility = android.view.View.GONE
                    }
                }
                
                // 显示信号原因
                if (!stock.signalReason.isNullOrEmpty()) {
                    binding.signalReasonTextView.visibility = android.view.View.VISIBLE
                    binding.signalReasonTextView.text = stock.signalReason
                } else {
                    binding.signalReasonTextView.visibility = android.view.View.GONE
                }
            } else {
                binding.signalBadge.visibility = android.view.View.GONE
                binding.buyPriceLayout.visibility = android.view.View.GONE
                binding.priceTargetsLayout.visibility = android.view.View.GONE
                binding.signalReasonTextView.visibility = android.view.View.GONE
            }
            
            binding.root.setOnClickListener {
                onItemClick(stock)
            }
        }
    }
    
    class StockDiffCallback : DiffUtil.ItemCallback<Stock>() {
        override fun areItemsTheSame(oldItem: Stock, newItem: Stock): Boolean {
            return oldItem.symbol == newItem.symbol
        }
        
        override fun areContentsTheSame(oldItem: Stock, newItem: Stock): Boolean {
            return oldItem == newItem
        }
    }
}

